package com.yoomiito.app.ui.order.detail;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yoomiito.app.R;
import com.yoomiito.app.model.order.OrderDetailGoods;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.AllUtils;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.utils.YMToast;
import com.yoomiito.app.widget.RoundImageView;
import com.yoomiito.app.widget.SpacingDecoration;

import java.util.List;

public class OrderInfoAdapter extends BaseQuickAdapter<OrderDetailGoods, BaseViewHolder> {

    public OrderInfoAdapter(@Nullable List<OrderDetailGoods> data) {
        super(R.layout.item_order_detail_goods, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderDetailGoods item) {
        RoundImageView roundImageView = helper.getView(R.id.item_order_goods_iv);
        ImageLoader.getInstance().into(roundImageView.getContext(), item.getProduct_img(), roundImageView);
        helper.setText(R.id.item_order_goods_title, item.getProduct_name());
        TextView statusTv = helper.getView(R.id.order_status);
        statusTv.setText(item.getStatusStr());
        statusTv.setTextColor(ResUtil.getColor(7 == item.getStatus() ? R.color.color_balk_999999 : R.color.color_orange));
        helper.setText(R.id.item_order_goods_price, "¥"+item.getAmount());
        helper.setText(R.id.item_order_goods_size, "x" + item.getNum());
        helper.setText(R.id.order_id, "订单编号：" + item.getSn());
        helper.setText(R.id.order_notes, "买家备注：" + item.getNote());
        helper.setVisible(R.id.order_notes, !TextUtils.isEmpty(item.getNote()));
        helper.setGone(R.id.copy, item.getExpressState() == 1);
        helper.setGone(R.id.get_wuliu_xin, item.getExpressState() == 1);
        helper.setGone(R.id.wuliu_company, item.getExpressState() == 1);
        helper.setGone(R.id.kuaidi_id, item.getExpressState() == 1);
        helper.setGone(R.id.wuliu_info_new, item.getExpressState() == 1);
        if (item.getExpressState() == 1) {
            helper.setText(R.id.wuliu_company, "物流公司：" + item.getExpress_company());
            helper.setText(R.id.kuaidi_id, "快递单号：" + item.getTrack_no());
            if (item.getExpressInfo() != null && item.getExpressInfo().size() > 0) {
                helper.setText(R.id.wuliu_info_new, "最新物流信息：" + item.getExpressInfo().get(0).getContext());
            }
        }
        helper.getView(R.id.copy).setOnClickListener(v -> {
            AllUtils.copyText(mContext, item.getTrack_no());
            YMToast.showText("复制成功");
        });
        helper.getView(R.id.get_wuliu_xin).setOnClickListener(v -> {
            WuLIuActivity.start(mContext, item);
        });
        RecyclerView rcy = helper.getView(R.id.rcy);
        rcy.setLayoutManager(new LinearLayoutManager(mContext));
        if (rcy.getItemDecorationCount() >0){//避免重复添加itemDecoration
            RecyclerView.ItemDecoration itemDecoration = rcy.getItemDecorationAt(0);
            if(itemDecoration == null){
                rcy.addItemDecoration(new SpacingDecoration(0, DensityUtil.dp2px(10),false));
            }
        }else{
            rcy.addItemDecoration(new SpacingDecoration(0, DensityUtil.dp2px(10),false));
        }
        rcy.setAdapter(new OrderInfoJiaMaiCodeAdapter(item.getAcode()));
    }
}
